<?php

//根据当前用户的微博信息判断推荐游戏好友

include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/config/city_config.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tuijian/inc.php");


//所有的参数
$g_cgival 	= array();
//存储中间数据 非输出数据 可以不用
$g_pro	 	= array();
//要显示的数据 输出数据
$g_show 	= array();

function check_cgi_pro()
{		
	global $g_cgival, $g_pro, $g_show, $config;
	
	cgi::both($g_cgival["p"], "p", "");
	$g_cgival["p"] = intval($g_cgival["p"]);
	
	if($g_cgival["p"] < 1) $g_cgival["p"]=1;
	if($g_cgival["p"] > 50) $g_cgival["p"]=50;
	cgi::both($g_cgival["pcount"], "pcount", 20);
	$g_cgival["pcount"] = intval($g_cgival["pcount"]);

	
	cgi::both($g_cgival["topic_id"], "topic_id", "");
	$g_cgival["topic_id"] = intval($g_cgival["topic_id"]);
}


function get_data()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	//登录相关
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		
		$current_user_info = 		$user_detail_info = $user_info->get_cur_user_info();
		$uid = $user_detail_info["uid"];
		$user_nickname = $user_detail_info["user_nickname"];
		$g_show["is_logined"] = true;
		$g_show["current_user_info"] = $user_detail_info;
	}
	else
	{
		$url = "/user/login.php?url={$_SERVER["REQUEST_URI"]}";
		header("Location:$url");
	}	
	
	$db_tuijian_unlike = new db_tuijian_unlike();
	$unlike_list = $db_tuijian_unlike->get_list_by_uid($uid);
	
	$ids = array();
	foreach($unlike_list as $key => $value)
	{
		$ids[] = $value["unlike_uid"];
	}
	
	$db_user_search = new db_user_search();
	
	$school_id = $current_user_info["school_id"];
	$province_id = $current_user_info["province"];
	$city_id = $current_user_info["city"];
	//$start_year = $current_user_info["start_year"];
	
	
	$db_user_search = new db_user_search();
	$db_user_info = new db_user_info();
	$topic_id= $g_cgival["topic_id"] ;
	if(!$topic_id)
	{
		throw new Exception("topic_id 不能为空！");
	}
	
	$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
	$topic_item = $db_mblog_cron_topic_tag->get_mblog_cron_topic_tag_item_by_id($topic_id);
	$g_show["topic_item"] = $topic_item;
	
	//$count = $db_user_search->get_user_count_by_search($search_key,"","",$topic_id,$sex,$verified_type,$school_id,$start_year);
	$list = $db_user_search->get_user_list_by_search(1,$g_cgival["pcount"],$search_key,"","",$topic_id,$sex,$verified_type,$school_id,$start_year,$ids);
		
	
//	$count1 = $db_user_search->get_user_count_by_search($search_key,$province_id,"",$topic_id,$sex,220,"","");
	$list1 = $db_user_search->get_user_list_by_search(1,$g_cgival["pcount"],$search_key,$province_id,"",$topic_id,$sex,220,"","",$ids);
	
	$list2 = $db_user_search->get_user_list_by_search(1,$g_cgival["pcount"],$search_key,$province_id,"",$topic_id,$sex,0,"","",$ids);
	
	$mblog_cache_v2 = new mblog_cache_v2();
	

	if($list)
	{
		$uids = array();
		foreach($list as $key => $value)
		{
			$list[$key]["tuijian_reason"] = "TA是你{$list[$key]["start_year"]}级的校友";
			$uids[] = $value["uid"];
		
		}
		$uids = join(",",$uids);
		$user_list = $mblog_cache_v2->cache_api_get_user_info_by_uids($uids,$screen_name="",$trim_status=1,$has_extend=1);
		$sina_user_info =array();
		foreach($user_list['data']["result"] as $key => $value)
		{
			$sina_user_info["{$value["id"]}"] = $value;
		}
	}
	
	
	
	$uids = array();
	foreach($list1 as $key => $value)
	{
		$list1[$key]["tuijian_reason"] = "TA是你附近的达人";
		$uids[] = $value["uid"];
	
	}
	$uids = join(",",$uids);
	$user_list = $mblog_cache_v2->cache_api_get_user_info_by_uids($uids,$screen_name="",$trim_status=1,$has_extend=1);
	
	foreach($user_list['data']["result"] as $key => $value)
	{
		$sina_user_info["{$value["id"]}"] = $value;
	}
	
	$uids = array();
	foreach($list2 as $key => $value)
	{
		$list2[$key]["tuijian_reason"] = "TA是你附近的名人";
		$uids[] = $value["uid"];
	
	}
	$uids = join(",",$uids);
	$user_list = $mblog_cache_v2->cache_api_get_user_info_by_uids($uids,$screen_name="",$trim_status=1,$has_extend=1);
	
	foreach($user_list['data']["result"] as $key => $value)
	{
		$sina_user_info["{$value["id"]}"] = $value;
	}
	
	//合并list
	$list = array_merge($list,$list1,$list2);
	foreach($list as $key => $value)
	{
		$list[$key]["location"] = $config["province"][$value["province"]]["name"]." ".$config['city'][$value["province"]][$value["city"]];
		$list[$key]["detail_info"] = $sina_user_info["{$value["uid"]}"];
	}
	
	
	
	$g_show["list"] = $list;
	
	
 	$g_show["page_title"] ="用户搜索";
 	
 	
 	#############省配置
 	$province = array();
 	foreach($config['province'] as $key => $value)
 	{
 		$province[] = array("id"=>$key,"name"=>$value["name"]);
 	}
 
 	$g_show["province"] = $province;
 	
	#############市配置
	$city_list= array();
	
	if($province_id)
	{
		foreach($config['city'][$province_id] as $key => $value)
		{
			$city_list[] = array("id"=>$key,"name"=>$value);
		}
		$g_show["city_list"] = $city_list;
	}
	
 	
	//类型1
	$mod_mblog = new mod_mblog();
	list($list1,$list2,$list3,$list4) = $mod_mblog->get_topic_list_order_by_like();
	$g_show["list1"] = $list1;
	$g_show["list2"] = $list2;
	$g_show["list3"] = $list3;
	$g_show["list4"] = $list4;
 	
 	
 	#############游戏话题配置
 	$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
 	$topic_list = $db_mblog_cron_topic_tag->get_mblog_topic_list_need_cron(1,20,array(1,2,3));
 	$g_show["topic_list"] = $topic_list;
 	$g_show["province_id"] = $province_id;//$user_detail_info["province"];
 	$g_show["city_id"] = $city_id;//$user_detail_info["city"];
 	$g_show["key"] =$search_key;
 
 	$g_show["sex"] =$sex;
 	$g_show["topic_id"] =$topic_id;
 	$g_show["verified_type"] =$verified_type;
 	
}

try {
	$g_show['error'] = 0;
	$g_show['errmsg'] = "";
	check_cgi_pro();
	get_data();
	
} catch (Exception $e) {
	$g_show['error'] = 1;
	$g_show['errmsg'] = $e->getMessage();
}

show_pro();


exit;

function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;

	$smarty = p_get_smarty();	
	$smarty->assign("g_show", $g_show);
	if (isset($_GET["debug"]))
	{
		print_r($g_show);
	}
	$smarty->display("search/like.html");
	
	unset($g_cgival);
	unset($g_pro);
	unset($g_show);
	unset($config);
	
}

